package top.oauth2.server.controller;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.oauth2.common.constant.RedisConstant;
import top.oauth2.common.response.Message;
import top.oauth2.common.response.ResponseMessage;
import top.oauth2.common.util.captcha.ImageCaptchaParams;
import top.oauth2.common.util.captcha.ImageCaptchaResult;
import top.oauth2.common.util.captcha.ImageCaptchaUtil;
import top.oauth2.common.util.captcha.SmsCaptchaUtil;
import top.oauth2.server.cilent.SmsClient;
import top.oauth2.server.entity.SendSmsVo;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author xphu
 * @version 1.0
 * @date 2021/9/8 16:57
 */
@Slf4j
@RequestMapping("/captcha")
@RestController
public class CodeCaptchaController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 短信服务
     */
    @Autowired
    SmsClient smsClient;

    /**
     * 获取验证码
     * @throws IOException
     */
    @GetMapping("/img")
    public Message getImgCaptchaSaveSession() throws IOException {
        // 获取redis key用来保存验证码
        String codeKey = IdUtil.simpleUUID();
        // 获取验证码对象
        ImageCaptchaResult imageCaptchaResult = ImageCaptchaUtil.createBase64(new ImageCaptchaParams());
        // 获取图片Base64
        String img = imageCaptchaResult.getReturnContent();
        // 获取验证码文字
        String words = imageCaptchaResult.getWords();
        // 保存到redis
        redisTemplate.opsForValue().set(codeKey, words, 60, TimeUnit.SECONDS);
        log.info("key：{}, 获取图片验证码：{}", codeKey, words);
        return ResponseMessage.builder()
                .success("成功")
                .put("img", img)
                .put("codeKey", codeKey)
                .message();
    }


    /**
     * 获取验证码
     * @throws IOException
     */
    @GetMapping("/sms")
    public Message getSmsCaptchaSaveSession(String phone) throws IOException {
        String redisKey = RedisConstant.SMS_PHONE_STR_KEY + phone;
        // 添加国内标记
        if (!phone.contains("+86")){
            phone = "+86" + phone;
        }
        // 获取验证码文字
        String code = SmsCaptchaUtil.getCode();
        // 保存到redis
        redisTemplate.opsForValue().set(redisKey, code, 5, TimeUnit.MINUTES);
        // todo 调用短信接口发送短信
        log.info("key：{}, 获取手机验证码：{}", phone, code);
        return smsClient.sendSmsMsg(new SendSmsVo(phone, code, "1123927", "TX"));
    }

    @GetMapping("redis")
    public void redis(){
        // str
//        redisTemplate.opsForValue().set(redisKey, code, 5, TimeUnit.MINUTES);
        // hash2
        redisTemplate.opsForHash().put("hash_test", "2", "占山");
        redisTemplate.expire("hash_test",5, TimeUnit.MINUTES);
    }

}
